<!-- 地址管理 -->
<template>
  <view v-if="shenheStatus != 1 || !shenheAliStatus" class="address">
    <!-- 地址列表 -->
    <view class="address-all">
      <view class="address-list" v-for="(item, index) in list" :key="index">
        <view class="address-list-top" @click="selectAddress(index)">
          <view>
            <view>{{ $t('clerk.consigneePerson') }}{{ item.name }}</view>
            <view>{{ item.mobile }}</view>
          </view>
          <view>{{ item.address }}</view>
        </view>
        <view
          class="address-list-bottom"
          :style="{ justifyContent: 'space-between' }"
        >
          <view
            class="list-bottom-left"
            @click="setDefaultAdd(item.id)"
            v-if="item.is_default == 1"
          >
            <image
              style="background: #ffffff"
              :src="asyncImgs.history.checked"
              mode="aspectFit"
            ></image>
            <view>{{ $t('my.defaultAddress') }}</view>
          </view>
          <view class="list-bottom-left" @click="setDefaultAdd(item.id)" v-else>
            <!-- <image style="background: #FFFFFF;" :src="asyncImgs.history.unchecked" mode="aspectFit"></image> -->
            <image :src="asyncImgs.history.unchecked" mode="aspectFit"></image>
            <view style="color: #333333">{{ $t('my.default') }}</view>
          </view>
          <view class="list-bottom-right">
            <view @click="jump(item.id)">
              <u-icon name="edit-pen"></u-icon>
              <view>{{ $t('cart.edit') }}</view>
            </view>
            <view @click="delItem(item.id)">
              <u-icon name="trash"></u-icon>
              <view>{{ $t('cart.delete') }}</view>
            </view>
          </view>
        </view>
      </view>
      <view v-if="list.length == 0" style="padding-top: 30rpx">
        <u-empty :text="$t('common.noAddress')" mode="address"></u-empty>
      </view>
    </view>
    <!-- 地址列表 -->

    <!-- 底部新增收货地址内容 -->
    <view class="address-bottom">
      <view
        ><navigator class="add" url="/user/my/addreceiving" open-type="navigate"
          >+ {{ $t('my.creatAddress') }}</navigator
        ></view
      >
    </view>
    <!-- 底部新增收货地址内容 -->
  </view>
  <Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
import Shenhe from '@/components/shenhe/shenhe';
export default {
  components: { Shenhe },
  data() {
    return {
      list: [],
      is_order: 0,
    };
  },
  onShow() {
    this.getAddressList();
  },
  onLoad(t) {
    if (t.is_order) {
      this.is_order = 1;
    }
    uni.setNavigationBarTitle({
      title: this.$t('title.addressManage'),
    });
  },
  methods: {
    jump(id) {
      this.$utils.toUrl('/user/my/addreceiving?id=' + id, 'navigate');
    },
    async setDefaultAdd(id) {
      try {
        await this.$allrequest.user
          .address_set_default({
            address_id: id ? id : this.address_id,
          })
          .then(res => {
            if (res.code == 0) {
              this.getAddressList(0);
            }
          });
      } catch (e) {
        //TODO handle the exception
      }
    },
    async delItem(id) {
      try {
        await this.$allrequest.user.address_del({ address_id: id });
        this.getAddressList();
      } catch (e) {
        //TODO handle the exception
      }
    },
    async getAddressList() {
      try {
        const res = await this.$allrequest.user.address_list();
        this.list = res.data.list;
      } catch (e) {
        //TODO handle the exception
      }
    },
    //进去新增收货人页面
    goaddreceiving() {
      uni.navigateTo({
        url: '/pages/my/addreceiving',
      });
    },
    selectAddress(index) {
      if (this.is_order) {
        uni.setStorageSync(
          'SELECT_ORDER_ADDRESS',
          JSON.stringify(this.list[index])
        );
        uni.navigateBack({
          delta: 1,
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.address {
  padding-top: 20rpx;
  min-height: 100vh;
  padding-bottom: 306rpx;
  background-color: #f2f2f2;
}
.address-list {
  margin-bottom: 20rpx;
  background-color: #ffffff;
  border-radius: 24rpx;
  padding: 0rpx 30rpx;
  .address-list-top {
    border-bottom: 1rpx solid #f0f0f0;
    padding: 34rpx 0rpx;
    > view:first-child {
      display: flex;
      justify-content: space-between;
      align-items: flex-end;
      view:first-child {
        font-size: 32rpx;
        color: #262626;
        font-weight: bold;
      }
      view:last-child {
        font-size: 30rpx;
        color: #262626;
      }
    }
    > view:last-child {
      margin-top: 18rpx;
      font-size: 24rpx;
      color: #999999;
    }
  }
  .address-list-bottom {
    height: 80rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .list-bottom-left {
      display: flex;
      align-items: center;
      image {
        width: 26rpx;
        height: 26rpx;
        border-radius: 50%;
      }
      view {
        margin-left: 12rpx;
        font-size: 24rpx;
        color: #f2270c;
      }
    }
    .list-bottom-right {
      display: flex;
      align-items: center;
      > view {
        margin-left: 56rpx;
        display: flex;
        align-items: center;
        view {
          margin-left: 12rpx;
          font-size: 24rpx;
          color: #262626;
        }
        image {
          width: 24rpx;
          height: 24rpx;
          background-color: red;
        }
      }
    }
  }
}
.address-bottom {
  position: fixed;
  left: 0rpx;
  bottom: 0rpx;
  width: 100%;
  background-color: #f2f2f2;
  > view:first-child {
    padding: 0rpx 36rpx;
    height: 146rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #ffffff;
    view:first-child {
      width: 322rpx;
      height: 94rpx;
      border: 1rpx solid #c7c7c7;
      text-align: center;
      line-height: 94rpx;
      font-size: 32rpx;
      color: #262626;
      font-weight: bold;
      border-radius: 48rpx;
    }
    view:last-child {
      border-radius: 48rpx;
      width: 324rpx;
      height: 96rpx;
      color: #ffffff;
      text-align: center;
      line-height: 96rpx;
      font-size: 32rpx;
      font-weight: bold;
      background: linear-gradient(90deg, #f3150c, #f14b0c);
    }
  }
  > view:last-child {
    padding: 0rpx 36rpx;
    height: 146rpx;
    background-color: #ffffff;
    margin-top: 14rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    .add {
      width: 100%;
      height: 96rpx;
      line-height: 96rpx;
      text-align: center;
      font-size: 32rpx;
      color: #ffffff;
      font-weight: bold;
      background: linear-gradient(90deg, #f3150c, #f14b0c);
      border-radius: 48rpx;
    }
  }
}
</style>
